const washDao = require('../dao/wash');
const userDao = require('../dao/user');

const Response = require('../entity/http/Response');

const washService = {
    async getAllWashRecord(reqId, uid) {
        let result;
        try {
            // 查看当前用户类型 - 用于细分权限
            let daoFunc;
            let selfUserType;
            let queryResult = await userDao.getUserType(reqId, uid);
            if (queryResult.length) {
                selfUserType = queryResult[0].userType;
            } else {
                return new Response().fail(400, "疑似攻击 已记录到系统日志中");
            }
            if (selfUserType == 'department-staff') {
                daoFunc = washDao.getAllWashRecord_usePower;
            } else {
                daoFunc = washDao.getAllWashRecord;
            }
            // 获取所有洗消记录
            let dbResult = await daoFunc(reqId, uid);
            result = new Response().success(dbResult);
        } catch (err) {
            result = new Response().fail(500, "获取所有洗消记录失败");
        }
        return result;
    },
    async getWashRecordInfo(reqId, uid, washRecordId) {
        let result;
        try {
            // 查看当前用户类型 - 用于细分权限
            let daoFunc;
            let selfUserType;
            let queryResult = await userDao.getUserType(reqId, uid);
            if (queryResult.length) {
                selfUserType = queryResult[0].userType;
            } else {
                return new Response().fail(400, "疑似攻击 已记录到系统日志中");
            }
            if (selfUserType == 'department-staff') {
                daoFunc = washDao.getWashRecordInfo_usePower;
            } else {
                daoFunc = washDao.getWashRecordInfo;
            }
            // 获取洗消记录
            let dbResult = await daoFunc(reqId, uid, washRecordId);
            if (dbResult.length) {
                result = new Response().success(dbResult[0]);
            } else {
                result = new Response().fail(500, "不存在该洗消记录");
            }
        } catch (err) {
            result = new Response().fail(500, "获取洗消记录详情失败");
        }
        return result;
    },
    async updateWashRecord(reqId, uid, { id, doctorName, patientName, patientSex, patientID, diagnosisNumber, hospitalizationNumber, remark }) {
        let result;
        try {
            // 查看当前用户类型 - 用于细分权限
            let daoFunc;
            let selfUserType;
            let queryResult = await userDao.getUserType(reqId, uid);
            if (queryResult.length) {
                selfUserType = queryResult[0].userType;
            } else {
                return new Response().fail(400, "疑似攻击 已记录到系统日志中");
            }
            if (selfUserType == 'department-staff') {
                daoFunc = washDao.updateWashRecord_usePower;
            } else {
                daoFunc = washDao.updateWashRecord;
            }
            // 修改洗消记录
            let dbResult = await daoFunc(reqId, uid, id, doctorName, patientName, patientSex, patientID, diagnosisNumber, hospitalizationNumber, remark);
            if (dbResult.changedRows) {
                result = new Response().success("修改洗消记录成功");
            } else {
                result = new Response().fail(500, "修改前后洗消记录信息无变化");
            }
        } catch (err) {
            result = new Response().fail(500, "修改洗消记录失败");
        }
        return result;
    },
    async delWashRecordInfo(reqId, uid, washRecordId) {
        let result;
        try {
            let dbResult = await washDao.delWashRecordInfo(reqId, uid, washRecordId);
            if (dbResult.changedRows) {
                result = new Response().success("删除洗消记录成功");
            } else {
                result = new Response().fail(500, "不存在该洗消记录");
            }
        } catch (err) {
            result = new Response().fail(500, "删除洗消记录失败");
        }
        return result;
    }
};

module.exports = washService;